package com.ytrsoft.entity;

import com.ytrsoft.enums.MessageStatus;
import com.ytrsoft.enums.MessageType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

@Entity
@Schema(description = "消息类")
public class Message extends BaseEntity {

    @Schema(description = "发送者")
    @ManyToOne
    @JoinColumn(name = "from_id", nullable = false)
    private User from;

    @Schema(description = "接收者")
    @ManyToOne
    @JoinColumn(name = "to_id", nullable = false)
    private User to;

    @Schema(description = "传输内容")
    private String content;

    @Schema(description = "类型")
    @Enumerated(EnumType.STRING)
    private MessageType msgType;

    @Schema(description = "状态")
    @Enumerated(EnumType.STRING)
    private MessageStatus msgStatus;

    @Schema(description = "发送时间")
    private Date sentAt;

    @Schema(description = "文件名称")
    private String fileName;

    @Schema(description = "关联日程")
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name = "schedule_id", referencedColumnName = "id")
    private Schedule schedule;

    @Override
    public void prePersist() {
        super.prePersist();
        if (msgType != MessageType.TEXT && StringUtils.isEmpty(content)) {
            content = null;
        }
    }

    public User getFrom() {
        return from;
    }

    public void setFrom(User from) {
        this.from = from;
    }

    public User getTo() {
        return to;
    }

    public void setTo(User to) {
        this.to = to;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public MessageType getMsgType() {
        return msgType;
    }

    public void setMsgType(MessageType msgType) {
        this.msgType = msgType;
    }

    public MessageStatus getMsgStatus() {
        return msgStatus;
    }

    public void setMsgStatus(MessageStatus msgStatus) {
        this.msgStatus = msgStatus;
    }

    public Date getSentAt() {
        return sentAt;
    }

    public void setSentAt(Date sentAt) {
        this.sentAt = sentAt;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public Schedule getSchedule() {
        return schedule;
    }

    public void setSchedule(Schedule schedule) {
        this.schedule = schedule;
    }
}
